import os

from flask import request
from flask_admin import BaseView, expose

from dc_shop.admin_panel.app.constants import media_path


class UploadView(BaseView):
    @expose("/", methods=["GET", "POST"])
    def upload(self):
        if request.method == "POST":
            f = request.files.get("file")
            file_path = os.path.join(media_path, f.filename)
            f.save(file_path)
            # You can return a JSON response then get it on client side:
            # (see template index.html for client implementation)
            # return jsonify(uploaded_path=file_path)
        return self.render("file/upload.html")
